<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>普通用户信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <!-- <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.deptName" placeholder="输入部门名称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.deptNo" placeholder="输入部门代码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form> -->
    <!--列表-->
    <el-button type="warning" @click="addUser" style="margin-bottom: 20px">添加用户</el-button>
    <el-table size="small" :data="userList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <!-- <el-table-column align="center" type="selection" width="60">
      </el-table-column> -->
      <el-table-column sortable align="center" prop="nickName" label="用户昵称" width="200">
      </el-table-column>
      <el-table-column align="center" prop="headImg" label="头像" width="200">
        <template slot-scope="scope" v-if="scope.row.headImg">
          <el-image
            style="width: 100px"
            :src="scope.row.headImg"
            fit="contain"></el-image>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" prop="phone" label="手机号" width="200">
      </el-table-column>
      <el-table-column sortable align="center" prop="password" label="密码" width="100">
      </el-table-column>
      <el-table-column sortable align="center" prop="roles" label="用户等级" width="100">
        <template slot-scope="scope">
          <div>{{scope.row.roles | levelFilter}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="editUser(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageQuery" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="editTitle" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="用户昵称" prop="nickName">
          <el-input size="small" v-model="editForm.nickName" auto-complete="off" placeholder="请输入用户昵称"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="headImg">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :on-change="handleChange"
            :auto-upload="false"
            :before-upload="beforeAvatarUpload">
            <img v-if="editForm.headImg" :src="editForm.headImg" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input size="small" v-model="editForm.phone" auto-complete="off" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input size="small" v-model="editForm.password" auto-complete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm(editOrAdd ? 'editForm' : 'addForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
export default {
  name:'User',
  data() {
    return {
      editOrAdd: true, // 编辑 / 新增 => true / false
      userList: [], //用户列表
      pageQuery: {
        pageNum: 1,
        pageSize: 10
      },
      loading: false, //是显示加载
      editFormVisible: false, // 编辑框是否弹出
      editTitle: '编辑用户',
      editForm: {
        id: '',
        nickName: '',
        headImg: '',
        phone: '',
        password: ''
      },
      // rules表单验证
      rules: {
        nickName: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' }
        ],
        phone: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
        password: [{ required: true, message: '请输入用户密码', trigger: 'blur' }]
      },
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  filters: {
    levelFilter (val) {
      switch (val) {
        case 'ROLE_USER':
          return '用户'
          break
        case 'ROLE_ADMIN':
          return '管理员'
          break
        default:
          return '管理员'
          break
      }
    }
  },
  created() {
    this.getUserList(this.pageQuery)
  },
  methods: {
    /**
     * 编辑弹框保存用户修改信息
     */
    submitForm (msg) {
      if (msg === 'editForm') {
        this.$api.auth.discUser(this.editForm).then(res => {
          if (res.code === 200) {
            this.editFormVisible = false
            this.getUserList(this.pageQuery)
          }
        }).catch(err => this.editFormVisible = false)
      } else {
        delete this.editForm.id
        this.$api.auth.register(this.editForm).then(res => {
          if (res.code === 200) {
            this.editFormVisible = false
            this.getUserList(this.pageQuery)
          }
        })
      }
    },
    /**
     * 获取用户列表
     */
    getUserList(pageQuery) {
      this.loading = true
      this.$api.auth.getUserList(pageQuery).then(res => {
        if (res.code === 200) {
          this.userList = res.data.records
          this.loading = false
        }
      }).catch(err => this.loading = false)
    },
    /**
     * 分页插件事件
     */
    callFather(parm) {
      this.pageQuery.pageNum = parm.currentPage
      this.pageQuery.pageSize = parm.pageSize
      this.getUserList(this.pageQuery)
    },
    /**
     * 关闭编辑、增加弹出框
     */
    closeDialog() {
      this.editFormVisible = false
    },
    /**
     * 编辑用户
     */
    editUser (user) {
      this.editOrAdd = true
      this.editFormVisible = true
      this.editTitle = "编辑用户"
      this.editForm.id = user.id
      this.editForm.nickName = user.nickName
      this.editForm.headImg = user.headImg
      this.editForm.phone = user.phone
      this.editForm.password = user.password
    },
    /**
     * 增加用户
     */
    addUser () {
      this.editOrAdd = false
      this.editFormVisible = true
      this.editTitle = "添加用户"
      this.editForm.nickName = ''
      this.editForm.headImg = ''
      this.editForm.phone = ''
      this.editForm.password = ''
    },
    /**
     * 删除用户
     */
    deleteUser (index, user) {
      this.$api.auth.deleteUser({id:user.id}).then(res => {
        if (res.code === 200) {
          this.getUserList(this.pageQuery)
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.editForm.headImg = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    /****
     * 上传图片 起步函数
     */
    handleChange (file) {
      this.submitUpload(file)
    },
    /***
     * 上传图片 上传函数
     */
    submitUpload (file) {
        let formData = new FormData() //  用FormData存放上传文件
        formData.append('file', file.raw, file.raw.name)
        this.$api.auth.uploadImage(formData).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '上传成功!'
            })
            this.editForm.headImg = res.data
          }
        })
    },
  }
}
</script>

<style>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

 
 